<template>
  <div class="article-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- <van-cell
        v-for="(article, index) in articles"
        :key="index"
        :title="article.title"
      /> -->
      <Articleitem
        v-for="(article, index) in articles"
        :key="index"
        :article="article"
      ></Articleitem>
    </van-list>
  </div>
</template>

<script>
import { getChannelById } from '@/api/article'
import Articleitem from '@/components/article-item'

export default {
  components: {
    Articleitem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      articles: [],
      loading: false,
      finished: false,
      timestamp: 0,
      refreshing: false
    }
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      const { data } = await getChannelById({
        channel_id: this.channel.id,
        timestamp: this.timestamp === 0 ? Date.now() : this.timestamp,
        with_top: 0
      })
      const result = data.data.results
      this.timestamp = data.data.pre_timestamp || 0
      result.forEach((item) => {
        this.articles.push(item)
      })
      this.loading = false
      // 如果请求不到数据了，就结束请求
      if (this.timestamp === 0) this.finished = true
    }
  }
}
</script>

<style lang="less" scoped>
.article-list {
  position: fixed;
  right: 0;
  left: 0;
  bottom: 50px;
  top: 90px;
  overflow-y: auto;
}
</style>
